<?php
class Server{
	public static function remove_server($box){
		$tablength = count($box);
		$i = 0;
		while ($i!=$tablength)
		{
			mysql_query("DELETE FROM ".DATABASE_TABLE_SERVER." WHERE `id` = $box[$i]") or die(mysql_error());
			$i++;
		}
	}
	
	public static function add_server($name,$address,$pin){
		$name = mysql_real_escape_string(htmlspecialchars($name));
		$address = mysql_real_escape_string(htmlspecialchars($address));
		$pin = mysql_real_escape_string(htmlspecialchars($pin));
		
		$sql_query=mysql_query("Select name,pin,address from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
		while ($query = mysql_fetch_array($sql_query) ){
			if ($query['pin'] == $pin){
				$_SESSION['error'] = "Pin déja existant";
				return false;
			}elseif ($query['name'] == $name){
				$_SESSION['error'] = "name déja existant";
				return false;
			}elseif ($query['address'] == $address){
				$_SESSION['error'] = "address déja existant";
				return false;
			}
		}
		mysql_query("INSERT INTO ".DATABASE_TABLE_SERVER." VALUES(LAST_INSERT_ID() ,'$name', '$address','$pin')");
		return true;
	}

	public static function check_pin_serv($getpin){
		$sql_query = mysql_query("Select name,pin from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
			while ($query = mysql_fetch_array($sql_query)){
				if (md5($query['pin']) == $getpin){
					return $query['name'];	
				}
			}
			add_log("Bad pin ".$_SERVER['REMOTE_ADDR'] );
			return false;
	}


}
?>